Skip to content
This repository was archived by the owner on Oct 6, 2026. It is now read-only.

[Feature] Add sanitize shop param utility - #483

Merged
NabeelAhsen merged 1 commit into
add-session-token-utilitesfrom
add-sanitize-shop-param-utility
Mar 17, 2021
Merged

NabeelAhsen merged 1 commit into
add-session-token-utilitesfrom
add-sanitize-shop-param-utility

Conversation

@NabeelAhsen

@NabeelAhsen NabeelAhsen commented Mar 12, 2021 •

Copy link
Copy Markdown
Contributor

Why are these changes introduced?

Depends on #479

This PR adds a sanitize_shop_domain utility method that can be used repeatedly throughout the lifecycle of an app's Oauth with Shopify.

This implementation has been ported from the shopify_app gem: https://github.com/Shopify/shopify_app/blob/17138d7d29d0c85d8f38ace981cac5f726b68833/lib/shopify_app/utils.rb#L4-L14

def sanitize_shop_domain(shop_domain, myshopify_domain="myshopify.com"):
    """Returns a sanitized Shopify shop domain.
    
    If the provided shop domain is invalid or could not be sanitized, returns None.
    
    Parameters:
        shop_domain: str
            A fully-qualified Shopify shop domain or hostname
        myshopify_domain: str, optional
            A custom Shopify domain
        
    Examples:
        >>> print(sanitize_shop_domain('my-shop'))
        'my-shop.myshopify.com'
        
        >>> print(sanitize_shop_domain('bad.com'))
        None
        
        >>> print(sanitize_shop_domain(shop_domain='my-shop', myshopify_domain='myshopify.io ))
        'my-shop.myshopify.io'
    """

Developers can use this like so:

from shopify.utils import shop_url
# ...

shop = shop_url.sanitize_shop_domain(params["shop"])
if not shop
    raise ValueError("Bad shop name given")

Things to focus on

  1. I'm unsure what type of errors can be thrown by urllib
  2. Do we encourage Pydocs on our methods? The description written above could have easily been described in the utils file as well.

Checklist

  • I have updated the CHANGELOG (if applicable)
  • I have followed the Shopify Python guide

@NabeelAhsen
NabeelAhsen changed the base branch from master to add-session-token-utilites March 12, 2021 17:47
@NabeelAhsen NabeelAhsen changed the title Add sanitize shop param utility [Feature] Add sanitize shop param utility Mar 12, 2021
@NabeelAhsen NabeelAhsen self-assigned this Mar 12, 2021
@NabeelAhsen
NabeelAhsen marked this pull request as ready for review March 12, 2021 20:05
Comment thread shopify/utils.py Resolved
@NabeelAhsen
NabeelAhsen force-pushed the add-sanitize-shop-param-utility branch from e650e5a to 7864dd7 Compare March 15, 2021 15:13
@NabeelAhsen
NabeelAhsen force-pushed the add-session-token-utilites branch from dca60e2 to e5e522e Compare March 15, 2021 15:13

@rezaansyed rezaansyed left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment thread shopify/utils/utilities.py Resolved
Comment thread shopify/utils/utilities.py Outdated
Comment on lines +70 to +72
if myshopify_domain not in name and "." not in name:
name += ".{domain}".format(domain=myshopify_domain)
name = re.sub(r"https?://", "", name)

uri = urlparse("http://{hostname}".format(hostname=name))

@paulomarg paulomarg Mar 15, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can go with a single RE here (untested):

Suggested change
if myshopify_domain not in name and "." not in name:
name += ".{domain}".format(domain=myshopify_domain)
name = re.sub(r"https?://", "", name)
uri = urlparse("http://{hostname}".format(hostname=name))
matches = re.match(r'(https?://)?({h})(\.{d})?'.format(h=HOSTNAME_PATTERN, d=myshopify_domain), shop_domain.lower().strip())
if matches:
name = matches.group(2) + myshopify_domain
return name

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was playing around with this:

print re.match(r'(https?://)?({h})(\.{d})?'.format(h=r"[a-z0-9][a-z0-9-]*[a-z0-9]", d='myshopify.com'), 'https://test.myshopify.com'.lower().strip()).group(1,2,3)
('https://', 'test', 'myshopify.com')
print re.match(r'(https?://)?({h})(\.{d})?'.format(h=r"[a-z0-9][a-z0-9-]*[a-z0-9]", d='myshopify.com'), 'https://test'.lower().strip()).group(1,2,3)
('https://', 'test', None)

Seems to work! (Note that I've changed the regex slightly from my initial suggestion)

@NabeelAhsen NabeelAhsen Mar 15, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unfortunately has trouble dealing with some of these bad input cases:

"myshop.com",
            "myshopify.com",
            "shopify.com",
            "two words",
            "store.myshopify.com.evil.com",
            "/foo/bar",
            "/foo.myshopify.io.evil.ru",
            "%0a123.myshopify.io ",
            "foo.bar.myshopify.io",

Edit: Sorry, this is replying to the first comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah the regexes above fail for the following inputs:

  • shopify.com: returns shopifymyshopify.com (expected None)
  • myshop.com: returns myshopmyshopify.com (expected None)
  • two words: returns twomyshopify.com (expected None)
  • foo.bar.myshopify.io: returns foomyshopify.com (expected None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, my regex didn't account for cases where the . in the domain isn't the start for .myshopify.com. That requirement is contextual, so we probably won't be able to handle it with a straight-up regexp. We could rewrite this code around a regex, but I think it would end up looking pretty much the same. LGTM!

@NabeelAhsen
NabeelAhsen force-pushed the add-sanitize-shop-param-utility branch from 7864dd7 to edfa11a Compare March 15, 2021 17:38
Comment thread shopify/utils/utilities.py Outdated
Comment on lines +70 to +72
if myshopify_domain not in name and "." not in name:
name += ".{domain}".format(domain=myshopify_domain)
name = re.sub(r"https?://", "", name)

uri = urlparse("http://{hostname}".format(hostname=name))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, my regex didn't account for cases where the . in the domain isn't the start for .myshopify.com. That requirement is contextual, so we probably won't be able to handle it with a straight-up regexp. We could rewrite this code around a regex, but I think it would end up looking pretty much the same. LGTM!

Comment thread test/utils/utilities_test.py Resolved
Comment thread test/utils/utilities_test.py Resolved
@NabeelAhsen
NabeelAhsen force-pushed the add-sanitize-shop-param-utility branch from edfa11a to 613f6c1 Compare March 15, 2021 18:25

@Paulinakhew Paulinakhew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool! 👌

@mllemango mllemango left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v cool

@NabeelAhsen
NabeelAhsen force-pushed the add-session-token-utilites branch from fbdf25a to c95ba2b Compare March 16, 2021 19:28
@NabeelAhsen
NabeelAhsen force-pushed the add-sanitize-shop-param-utility branch 2 times, most recently from 8dd0cf1 to 68fea6d Compare March 17, 2021 01:37
@NabeelAhsen
NabeelAhsen force-pushed the add-session-token-utilites branch from c95ba2b to 8bc8132 Compare March 17, 2021 02:51
@NabeelAhsen
NabeelAhsen force-pushed the add-sanitize-shop-param-utility branch from 68fea6d to 984849a Compare March 17, 2021 02:52
@NabeelAhsen
NabeelAhsen merged commit 203cde9 into add-session-token-utilites Mar 17, 2021
@NabeelAhsen
NabeelAhsen deleted the add-sanitize-shop-param-utility branch March 17, 2021 14:39
@NabeelAhsen NabeelAhsen mentioned this pull request Mar 17, 2021
2 tasks done
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants